#include "AvcMenu.h"

AvcMenu::AvcMenu (AvcLcd *l, AvcNav *n) {
  lcd = l;
  nav = n;
  currentMenu = 0;
  refreshTime = millis();
}

void AvcMenu::checkButtons (boolean refresh) {
  int8_t keyPressed = lcd->getKeyPressed();
  switch(keyPressed) {
    case KEY_MENU:
      currentMenu = (currentMenu + 1) % ENUM_COUNT;
      refresh = true;
      lcd->setMode(lcd->NONE);
      break;
    case KEY_SELECT:
      switch(currentMenu) {
        case GPS:
          nav->updateWaypoints();
          break;
        case SAMPLE:
          nav->startSampling(lcd);
          break;
        case RESET:
          nav->resetWaypoints();
          break;
        case SLIDESHOW:
          lcd->setMode(lcd->WAYPOINTS);
          break;
        case SPEED:
          nav->setMaxSpeed();
          break;
        case MENU_RUN_LOC:
          nav->nextRunLocation();
          break;
      }
      refresh = true;
  }
  if (refresh || (millis() - refreshTime) > 500 && lcd->getMode() == lcd->NONE) {
    refreshTime = millis();
    switch(currentMenu) {
      case GPS:
        lcd->printGps(nav->getLatitude(), nav->getLongitude(), nav->getHdop(), refresh, nav->getNextWaypoint());
        break;
      case SAMPLE:
        if (refresh) {
          lcd->printStartSampling(nav->getNumWaypoints());
        }
        break;
      case RESET:
        if (refresh) {
          lcd->askReset(nav->getNumWaypoints());
        }
        break;
      case SLIDESHOW:
        if (refresh) {
          lcd->askWaypointSlideshow(nav->getNumWaypoints());
        }
        break;
      case SPEED:
        lcd->askSetMaxSpeed(nav->getMaxSpeed(), refresh);
        break;
      case HEADING:
        lcd->trackHeading(nav->getHeadingToWaypoint(), nav->getHeading(), refresh);
        break;
      case OFFSET:
        lcd->askSetOffset(nav->getLatPotentialOffset(), nav->getLonPotentialOffset(), refresh);
        break;
      case MENU_RUN_LOC:
        if (refresh) {
          lcd->showRunLocation(nav->getRunLocation());
        }
        break;
    }
  }
}

